package com.pen.interfece.service.Impl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.pen.interfece.mapper.AdminMapper;
import com.pen.interfece.pojo.PageResult;
import com.pen.interfece.pojo.dto.AdminLoginDTO;
import com.pen.interfece.pojo.dto.AdminPageQueryDTO;
import com.pen.interfece.pojo.entity.Admin;
import com.pen.interfece.service.AdminService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.security.auth.login.AccountNotFoundException;
import java.util.List;

@Service
public class AdminServiceImpl implements AdminService {
    @Autowired
    private AdminMapper adminMapper;

    @Override
    public Admin login(AdminLoginDTO adminLoginDTO) {

        //1、根据用户名查询数据库中的数据
       Admin admin=adminMapper.getByAccount(adminLoginDTO);

       return admin;
    }

    @Override
    public void save(Admin admin) {
        admin.setPassword("123456");
        adminMapper.save(admin);
    }

    @Override
    public void deleteBatch(List<Long> ids) {
        for (Long id : ids) {
            adminMapper.deleteById(id);
        }
    }

    @Override
    public void update(Admin admin) {
        adminMapper.update(admin);
    }

    @Override
    public PageResult pageQuery(AdminPageQueryDTO adminPageQuerydto) {
        //开始分页查询，插件执行
        PageHelper.startPage(adminPageQuerydto.getPage(),adminPageQuerydto.getPageSize());
        Page<Admin> page=adminMapper.pageQuery(adminPageQuerydto);

        long total = page.getTotal();
        List<Admin> records = page.getResult();

        return new PageResult(total,records);
    }
}
